package com.dynamicallo.ty.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dynamicallo.ty.entity.Action;
import com.dynamicallo.ty.entity.Content;
import com.dynamicallo.ty.service.IActionService;
import com.dynamicallo.ty.service.IContentService;
import com.dynamicallo.ty.service.IInterbaleService;
import com.google.common.collect.Lists;

/**
 * 
 * @author wb
 * @date 2016年1月2日 下午6:17:37
 */
@Controller
@RequestMapping("/action")
public class ActionController {
    @Autowired
    private IActionService actionService;
    @Autowired
    private IInterbaleService intertableService;
    /**
     * jsp页面返回Action
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/showAction")
    public String toIndex(HttpServletRequest request,Model model){
        int actionId = Integer.parseInt(request.getParameter("actionId"));
        Action action = actionService.getActionById(actionId);
        model.addAttribute("action", action);
        return "showContent";
    }
    /**
     * 返回json化的全部content数据
     * @return
     */
    @RequestMapping(value="showActionJson",method=RequestMethod.GET )
    public ResponseEntity<List<Action>> listContent(){
        List<Action> temp = actionService.getAllAction();
        return  new ResponseEntity<List<Action>>(temp, HttpStatus.OK);
    }
    /**
     * 返回json化的部分content数据,依据id获取
     * @param request
     * @return
     */
    @RequestMapping(value="showContentJson",method=RequestMethod.GET ,params="one")
    public ResponseEntity<Action> listContent(@RequestParam("actionId") Integer actionId){
        if(actionId!=null){
            Action action = actionService.getActionById(actionId);
            return  new ResponseEntity<Action>(action, HttpStatus.OK); 
        }else{
            return new ResponseEntity<Action>(HttpStatus.EXPECTATION_FAILED);
        }
       
    }
    /**
     * 通过contentId查找其对应的所有的action
     * @param contentId
     * @return
     */
    @RequestMapping(value="getActionsByContentIdWithJson",method=RequestMethod.GET )
    public ResponseEntity<List<Action>> getActionsByContentIdWithJson(@RequestParam("contentId") Integer contentId){
        if(contentId!=null){
            //先从中间表找到对应的contentId 然后查找即可
            List<Integer> actionIds = intertableService.getAllActionByContentId(contentId);
            List<Action> temp =Lists.newArrayList();
            for(Integer i :actionIds){
                temp.add(actionService.getActionById(i));
            }
            return  new ResponseEntity<List<Action>>(temp, HttpStatus.OK); 
        }else{
            return new ResponseEntity<List<Action>>(HttpStatus.EXPECTATION_FAILED);
        }
    }
    /**
     * 通过contentId查找其对应的所有的action
     * @param contentId
     * @return
     */
    @RequestMapping(value="getActionsByContentIdWithJSP",method=RequestMethod.GET )
    public String getActionsByContentIdWithJSP(@RequestParam("contentId") Integer contentId,Model model){
        System.out.println(contentId);
        if(contentId!=null){
            //先从中间表找到对应的contentId 然后查找即可
            List<Integer> actionIds = intertableService.getAllActionByContentId(contentId);
            List<Action> actions =Lists.newArrayList();
            for(Integer i :actionIds){
                actions.add(actionService.getActionById(i));
            }
            model.addAttribute("actions", actions);
            System.out.println("添加actions:" + actions.toString());
            return  "/action/showActionByContentId"; 
        }else{
            return "/error";
        }
    }
}
